<template>
  <div class="lb-goods-list">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="输入查询" prop="name">
            <el-input
              v-model="searchForm.name"
              placeholder="请输入业务员姓名/昵称/手机号"
              style="width: 250px"
            ></el-input>
          </el-form-item>
          <el-form-item label="入驻时间" prop="start_time">
            <el-date-picker
              @change="getTableDataList(1)"
              v-model="searchForm.start_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="user_id" label="用户ID" fixed></el-table-column>
        <el-table-column prop="avatarUrl" label="微信头像" min-width="120">
          <template slot-scope="scope">
            <lb-image :src="scope.row.avatarUrl" />
          </template>
        </el-table-column>
        <el-table-column
          prop="nickName"
          label="微信昵称"
          :min-width="120"
        ></el-table-column>
        <el-table-column
          prop="user_name"
          label="业务员姓名"
          :min-width="120"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          :min-width="120"
        ></el-table-column>
        <el-table-column prop="sh_time" label="入驻时间" :min-width="120">
          <template slot-scope="scope">
            <block v-if="scope.row.sh_time !== 0">
              <p>{{ scope.row.sh_time | handleTime(1) }}</p>
              <p>{{ scope.row.sh_time | handleTime(2) }}</p>
            </block>
            <block v-else>-</block>
          </template>
        </el-table-column>
        <el-table-column prop="total_cash" label="累计佣金" :min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.total_cash}` }}
          </template>
        </el-table-column>
        <el-table-column prop="wallet_price" label="已提现" :min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.wallet_price}` }}
          </template>
        </el-table-column>
        <el-table-column prop="cash" label="可提现" :min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.cash}` }}
          </template>
        </el-table-column>
        <el-table-column prop="order_price" label="总成交金额" :min-width="120">
          <template slot-scope="scope">
            {{ `¥${scope.row.order_price}` }}
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      loading: false,
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > (moment(moment(Date.now()).format('YYYY-MM-DD')).unix() + 24 * 3600 - 1) * 1000
        }
      },
      searchForm: {
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        name: ''
      },
      tableData: [],
      total: 0
    }
  },
  created () {
    this.getTableDataList(1)
  },
  methods: {
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async toChange (index) {
      this.searchForm.status = index
      this.getTableDataList(1)
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.tableData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { start_time: time } = searchForm
      if (time && time.length > 0) {
        searchForm.start_time = time[0] / 1000
        searchForm.end_time = time[1] / 1000
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      let { code, data } = await this.$api.salesman.salesmanDataList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  .el-input {
    width: 200px;
  }
  .el-image {
    width: 120px;
    height: 120px;
  }
  .el-textarea {
    width: 600px;
  }
}
</style>
